//
//  PinDujiaTableViewCell.swift
//  XMYouPin
//
//  Created by 王宗成 on 2019/7/30.
//  Copyright © 2019 王宗成. All rights reserved.
//

import UIKit

class PinDujiaTableViewCell: WZC_TableViewCell {

    var model:PinRagModel?{
        
        didSet
        {
            guard let item = model else {
                
                return
            }
            
            self.bgImgView.kf.setImage(with: URL(string: item.pic_url ?? ""))
            self.titleLabel.text = item.title ?? ""
            self.subTitleLabel.text = item.subtitle ?? ""
            /*
             "pic_url": "https://img.youpin.mi-img.com/editor1/300d750ba878dec229cd7202a10cbec4.jpg?w=1080&h=540",
             "url": "",
             "title": "风懒懒的，这杯鸡尾酒却涩涩的",
             "subtitle": "在七月的尾巴上又想起你",
             */
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.hexColor(hexadecimal: "#f2f2f2")
        
        let imgRate:CGFloat =  1080.0/540.0
        let imgHeight:CGFloat =  (kScreenW-20)/imgRate
        
        self.bgImgView.snp.makeConstraints { (make) in
            
            make.top.equalToSuperview()
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.height.equalTo(imgHeight)
        }
        
        self.titleLabel.snp.makeConstraints { (make) in
            
            make.centerX.equalToSuperview()
            make.top.equalTo(self.bgImgView.snp_bottom).offset(5)
            make.height.equalTo(30)
        }
        
        self.subTitleLabel.snp.makeConstraints { (make) in
            
            make.centerX.equalToSuperview()
            make.top.equalTo(self.titleLabel.snp_bottom)
            
        }
        
        self.findButton.snp.makeConstraints { (make) in
            
            make.centerX.equalToSuperview()
            make.top.equalTo(self.subTitleLabel.snp_bottom).offset(8)
            make.size.equalTo(CGSize(width: 70, height: 25))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    private lazy var bgImgView : UIImageView = {
        let imageView = UIImageView()
        imageView.layer.cornerRadius = 4
        imageView.clipsToBounds = true
        self.contentView.addSubview(imageView)
        return imageView
    }()
    
    private lazy var titleLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.hexColor(hexadecimal: "#000000")
        label.textAlignment = NSTextAlignment.left
        self.contentView.addSubview(label)
        return label
    }()
    private lazy var subTitleLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.hexColor(hexadecimal: "#666666")
        label.textAlignment = NSTextAlignment.center
        self.contentView.addSubview(label)
        return label
    }()
//    124 86 48 149 149 149
    private lazy var findButton : UIButton = {
        
        let button = UIButton(type: .custom)
        button.setTitle("发现", for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.titleLabel?.font = Font(13)
        button.backgroundColor = RGB(r: 124, g: 86, b: 48)
        button.layer.cornerRadius = 4;
        button.addTarget(self, action: #selector(findAction), for: .touchUpInside)
        self.contentView.addSubview(button)
        return button
    }()
  
    @objc func findAction(){
        
        let vc:WZC_WebViewController = WZC_WebViewController()
        vc.urlString = model?.href
        self.viewController?.navigationController?.pushViewController(vc, animated: true)
    }
}
